//
//  WechatHelpers.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/17.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "WechatHelpers.h"
#import <AFNetworking.h>

#import "Utility.h"
#import "Constants.h"

#define WX_ACCESS_TOKEN @"access_token"
#define WX_OPEN_ID @"openid"
#define WX_REFRESH_TOKEN @"refresh_token"
#define WX_UNION_ID @"unionid"
#define WX_BASE_URL @"https://api.weixin.qq.com/sns"

@implementation WechatHelpers

+(void)getWXAccessTokenRequestkWithUrlString:(NSString*)subUrlString withHttpHeader:(NSDictionary*)headerDic withParas:(NSDictionary*)Paradic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@",subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain",nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    [manager GET:URL.absoluteString parameters:Paradic progress:^(NSProgress * _Nonnull downloadProgress) {
        ;
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject)
     {
         NSLog(@"请求access的response = %@", responseObject);
         NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];
         NSString *accessToken = [accessDict objectForKey:@"access_token"];
         NSString *openID = [accessDict objectForKey:@"openid"];
         NSString *refreshToken = [accessDict objectForKey:@"refresh_token"];
         // 本地持久化，以便access_token的使用、刷新或者持续
         if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
             [Utility setObject:accessToken key:kWXAccessToken];
             [Utility setObject:openID key:kWXOpenId];
             [Utility setObject:refreshToken key:kWXRefreshToken];
             [Utility setObject:accessToken key:kWXAccessToken];
             //             [[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里，来避免数据的丢失
         }
         successblock(accessDict);
     }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         failBlock(error);
     }];
}

+(void)getWXUserInfoRequestkWithUrlString:(NSString*)subUrlString withHttpHeader:(NSDictionary*)headerDic withParas:(NSDictionary*)Paradic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@",subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain",nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    [manager GET:URL.absoluteString parameters:Paradic progress:^(NSProgress * _Nonnull downloadProgress) {
        ;
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject)
     {
         successblock(responseObject);
     }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         failBlock(error);
     }];
}


@end
